package com.banksoft.cps.service.impl;

import com.banksoft.cps.bean.Company;
import com.banksoft.cps.bean.Device;
import com.banksoft.cps.bean.MonitorProduct;
import com.banksoft.cps.bean.query.DeviceQuery;
import com.banksoft.cps.bean.warpper.DeviceWarpper;
import com.banksoft.cps.dao.CompanyDao;
import com.banksoft.cps.dao.DeviceDao;
import com.banksoft.cps.dao.MonitorProductDao;
import com.banksoft.cps.service.DeviceService;
import com.banksoft.cps.util.UUIDFactory;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by rongzhao.zhu on 2018/1/2.
 */
@Service
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    DeviceDao deviceDao;

    @Autowired
    MonitorProductDao monitorProductDao;

    @Autowired
    CompanyDao companyDao;


    @Override
    public List<Device> list(DeviceQuery query) {
        return deviceDao.list(query);
    }

    @Override
    public List<DeviceWarpper> listWarpper(DeviceQuery query) {
        List<DeviceWarpper> deviceWarppers = new ArrayList<>();
        List<Device> deviceList = list(query);
        if(deviceList.isEmpty()){
            return null;
        }
        long now = System.currentTimeMillis();
        List<String> productIdList = new ArrayList<>();
        List<String> companyIdList = new ArrayList<>();
        Map<String,MonitorProduct>  pMap =  new HashMap<>();
        Map<String,Company> cMap = new HashMap<>();
        deviceList.forEach(e->{
            if(StringUtils.isNotBlank(e.getCompanyId())&&!companyIdList.contains(e.getCompanyId())){
                companyIdList.add(e.getCompanyId());
            }
            if(StringUtils.isNotBlank(e.getProductId())&&!productIdList.contains(e.getProductId())){
                productIdList.add(e.getProductId());
            }
            if(now - e.getUploadTime()>1000*60){
                e.setSsll("0");
            }
        });
        if(!productIdList.isEmpty()){
            List<MonitorProduct> products =  monitorProductDao.listByIdList(productIdList);
            products.forEach(e->pMap.put(e.getId(),e));
        }
        if(!companyIdList.isEmpty()){
            List<Company> companies = companyDao.listByIdList(companyIdList);
            companies.forEach(e->cMap.put(e.getId(),e));
        }
        deviceList.forEach(e->{
            DeviceWarpper warpper = new DeviceWarpper();
            BeanUtils.copyProperties(e,warpper);
            warpper.setCompany(cMap.get(e.getCompanyId()));
            warpper.setProduct(pMap.get(e.getProductId()));
            deviceWarppers.add(warpper);
        });

        return deviceWarppers;
    }

    @Override
    public long count(DeviceQuery query) {
        return deviceDao.count(query);
    }

    @Override
    public void save(Device bean) {

        if(StringUtils.isNotBlank(bean.getId())){
            deviceDao.update(bean);
        }else {
            bean.setId(UUIDFactory.random());
            bean.setCreateTime(System.currentTimeMillis());
            deviceDao.insert(bean);
        }
    }

    @Override
    public Device findById(String id) {
        if(StringUtils.isNotBlank(id)){
            DeviceQuery query = new DeviceQuery();
            query.setId(id);
            List<Device> list = deviceDao.list(query);
            if(!list.isEmpty()){
                long now = System.currentTimeMillis();
                Device e = list.get(0);
                if(now - e.getUploadTime()>1000*60){
                    e.setSsll("0");
                }
                return list.get(0);
            }

        }
        return null;
    }

    @Override
    public void delete(String id) {
        deviceDao.delete(id);
    }
}
